package com.ruoyi.util;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

public class OrderIdUtil {
    public  static String getOrderId(){
        // 获取当前日期 年月日
        String key = DateUtils.parseDateToStr("yyyyMMdd",DateUtils.getNowDate());
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        String number = redisCache.getCacheObject(key);
        // RedisTemplate template = redisCache.getRedisTemplate();

        String orderId = "";
        if (number == null || "".equals(number)){
            //第一次订单
            String num = String.format("%05d", 1);
            redisCache.setCacheObject(key,num,24, TimeUnit.HOURS);
            orderId = num;
        }else{
            int num = Integer.parseInt(number) + 1;
            String num1 = String.format("%05d", num);
            orderId = num1;
            redisCache.setCacheObject(key, num1);
        }
        return key+orderId;
    }
}
